<?php
/**
 * Description of OrderDAO
 * implement 5 functions: get, getList, add, edit, delete
 * @author: Beverley
 */

class OrderDAO {
    /**
    * Description of getOrder
    * return a order with specified id
    */
    public function getOrder($id) {
        $order = new Order();
        $row = $order->fetchRow("id = $id and status =".Constant::$STATUS_AVAILABLE);
        return $row;
    }

    /**
     * Description of getOrderList
     * return all orders available
     */
    public function getOrderListByUserID($user_id) {
       $order = new Order();
       $row = $order->fetchAll("user_id = $user_id and status =".Constant::$STATUS_AVAILABLE);
       return $row;
    }

    /**
     * Description of getOrderList
     * return all orders available
     */
    public function getOrderList() {
       $order = new Order();
       $row = $order->fetchAll($order->select()->where('status = ?',Constant::$STATUS_AVAILABLE));
       return $row;
    }

    /**
     * add a new order into Order table
     * @param <array> $data
     */
    public function addOrder($data) {
        $order = new Order();
        $order->insert($data);

        $db = Zend_Registry::get('db');
        // capture the id of the newly inserted record
        $id = $db->lastInsertId('order');
        return $id;
    }

    /**
     * edit specified order
     * @param <array> $array
     */
    public function editOrder($array) {
        $order = new Order();
        $where = $order->getAdapter()->quoteInto('id = ?', $array['id']);
        $order->update($array,$where);
    }

    /**
     * delete a order by set "status" is 0
     * @param <type> $id
     */
    public function deleteOrder($id) {
        $order = new Order();
        $data = array('status'=>Constant::$STATUS_DELETED); // change from unavailable to deleted
        $where = $order->getAdapter()->quoteInto('id = ?', $id);
        $order->update($data, $where);
    }
}
?>
